package com.cuitrip.business.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Space;

import com.cuitrip.app.MainApplication;
import com.cuitrip.business.login.model.ThirdAccount;
import com.cuitrip.business.login.proxy.LoginProxy;
import com.cuitrip.business.login.proxy.ThirdAccountProxy;
import com.cuitrip.business.user.model.CtUserInfo;
import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;
import com.facebook.appevents.AppEventsConstants;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.jumper.Jumper;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserEnterActivity extends CustomUiFragmentActivity {

    @Bind(R.id.third_login_layout_1)
    LinearLayout third_login_layout_1;

    @Bind(R.id.third_login_layout_2)
    LinearLayout third_login_layout_2;

    @Bind(R.id.btnGoogleplus)
    ImageView btnGoogleplus;

    @Bind(R.id.space)
    Space mSpace;
//    @Bind(R.id.btnFacebook)
//    ImageView btnFacebook;

    public static final int TAG_REQUEST_CODE = 0x01;
    private ThirdAccountProxy thirdAccountProxy = new ThirdAccountProxy();

    private ThirdAccountProxy.OnAuthListener onAuthListener = new ThirdAccountProxy.OnAuthListener() {
        @Override
        public void onComplete(final ThirdAccount thirdAccount) {
            showLoading(false);
            LoginProxy.getInstance().thirdLogin(thirdAccount, new LoginProxy.CallBack() {
                @Override
                public void onLoginSuccess(Object data) {
                    hideLoading();
                    if (data != null) {

                        if (data instanceof CtUserInfo) {
                            if (((CtUserInfo) data).isNewUser()) {
                                MainApplication.getInstance().logger.logEvent(AppEventsConstants.EVENT_NAME_COMPLETED_REGISTRATION);
                            }
                        }
                        LoginProxy.getInstance().reloginWithRestartActivity(UserEnterActivity.this);
                        finish();
                    }
                }

                @Override
                public void onLoginFail(int resultCode, String message) {
                    hideLoading();
                    if (resultCode == ThirdAccount.RESULT_NO_PHONE) {
                        Intent intent=new Intent(UserEnterActivity.this,BindEmailActivity.class).putExtra(BindEmailActivity.OBJECT_KEY, thirdAccount);
                        Jumper.launchActivityForResult(UserEnterActivity.this,intent, TAG_REQUEST_CODE);
                        //BindEmailActivity.lauchBindEmailActivity(UserEnterActivity.this, thirdAccount);
                        //finish();
                    } else {
                        ToastUtil.toast(getApplicationContext(), message, 0);
                    }

                }
            }, "");
        }

        @Override
        public void onCancel() {
            hideLoading();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==RESULT_OK){
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_enter);
        ButterKnife.bind(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        thirdAccountProxy.init(this);
        third_login_layout_1.setVisibility(I118Util.isSimplifiedChinese() ? View.VISIBLE : View.GONE);
        third_login_layout_2.setVisibility(!I118Util.isSimplifiedChinese() ? View.VISIBLE : View.GONE);
        btnGoogleplus.setVisibility(ThirdAccountProxy.isGoogleInstall(AppCoreRuntime.context) ? View.VISIBLE : View.GONE);
        mSpace.setVisibility(ThirdAccountProxy.isGoogleInstall(AppCoreRuntime.context)?View.VISIBLE:View.GONE);
    }



    @OnClick(R.id.btnSignIn)
    public void signIn() {
        LoginProxy.getInstance().gotoLogin(this);
    }

    @OnClick(R.id.btnSignUp)
    public void signUp() {
        LoginProxy.getInstance().gotoRegister(this);
    }

    @OnClick(R.id.btnLoginWeibo)
    public void weiboLogin() {
        thirdAccountProxy.authorizeThirdAccount(ThirdAccount.WEIBO,  onAuthListener);
    }

    @OnClick(R.id.btnLoginWechat)
    public void wechatLogin() {
        if (ThirdAccountProxy.isWechatInstall(AppCoreRuntime.context)) {
            thirdAccountProxy.authorizeThirdAccount(ThirdAccount.WE_CHAT,  onAuthListener);
        } else {
            ToastUtil.toast(R.string.social_wechat_not_installed);
        }
    }

    @OnClick(R.id.btnFacebook)
    public void facebookLogin() {
        thirdAccountProxy.authorizeThirdAccount(ThirdAccount.FACEBOOK,  onAuthListener);
    }

    @OnClick(R.id.btnGoogleplus)
    public void googleLogin() {

        thirdAccountProxy.authorizeThirdAccount(ThirdAccount.GOOGLE,  onAuthListener);

    }

    @OnClick(R.id.icon_clear)
    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public boolean initCloseTopBar() {
        return true;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        return new CustomUiConfig();
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
        thirdAccountProxy.destroy();
    }

    @Override
    protected void onStop() {
        super.onStop();
        thirdAccountProxy.onActivityStop();
    }

}
